package com.antstorm.statistics.util;

import java.time.*;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateTimeUtil {


    public static LocalDate getLocalDate() {
        return LocalDate.now();
    }


    public static LocalDateTime getLocalDateTime(){
        return LocalDateTime.now();

    }

    public static Date toDate(LocalDate date){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = date.atStartOfDay(zoneId);
        return  Date.from(zdt.toInstant());
    }

    public static LocalDate getLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }


    public static LocalDate addDaysToLocalDate(Long days) {
        return getLocalDate().plusDays(days);
    }

    /**
     * 获取 days天 之后的时间
     * @param days
     * @return
     */
    public static Date addDays(Long days) {
        LocalDate after = getLocalDate().plusDays(days);
        return toDate(after);
    }

    /**
     * 获取days天之前的时间
     * @param days
     * @return
     */
    public static Date reduceDays(Long days) {
        LocalDate after = getLocalDate().minusDays(days);
        return toDate(after);
    }


    /**
     *  LocalDateTime转为Date
     * @param time
     * @return
     */
    public static Date transToDate(LocalDateTime time){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = time.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * 获取minutes分钟之后的时间
     * @param minutes 要增加的分钟数
     * @return
     */
    public static Date addMins(long minutes) {
        LocalDateTime after=getLocalDateTime().plusMinutes(minutes);
        return transToDate(after);

    }
}
